package com.comleader.operator.common.util;

import static com.comleader.operator.validate.CommonValidator.CHINESE_NAME;
import static com.comleader.operator.validate.CommonValidator.addressPattern;

import java.util.List;
import java.util.regex.Matcher;

import com.comleader.operator.pojo.entity.IovCardEntity;
import com.comleader.operator.pojo.entity.PersonalUserEntity;
import com.comleader.operator.pojo.entity.SmsPortEntity;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @Dscription 脱敏实体对象工具类
 * @Author 连新景
 * @Date 2024/09/26 16:41
 */
public class DesensitizeEntityUtil {

    /**
     * @Description 脱敏个人用户敏感字段
     * @Auhtor 连新景
     * @Date 2024/09/26 15:15
     * @param list
     */
    public static void desensitizePersonalUser(List<PersonalUserEntity> list) {
        list.forEach(entity -> {
            // 用户号码
            if (Validator.isMobile(entity.getUserNumber())) {
                entity.setUserNumber(DesensitizedUtil.mobilePhone(entity.getUserNumber()));
            }
            // 机主姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getOwnerName())) {
                entity.setOwnerName(DesensitizedUtil.chineseName(entity.getOwnerName()));
            }
            // 机主身份证号
            if (IdcardUtil.isValidCard(entity.getOwnerIdNumber())) {
                entity.setOwnerIdNumber(DesensitizedUtil.idCardNum(entity.getOwnerIdNumber(), 6, 4));
            }
            // 机主证件地址
            String desensitizedOwnerIdAddress = desensitizeAddress(entity.getOwnerIdAddress());
            if (StrUtil.isNotBlank(desensitizedOwnerIdAddress)) {
                entity.setOwnerIdAddress(desensitizedOwnerIdAddress);
            }
            // 机主装机地址
            String desensitizedOwnerInstallAddress = desensitizeAddress(entity.getOwnerInstallAddress());
            if (StrUtil.isNotBlank(desensitizedOwnerInstallAddress)) {
                entity.setOwnerInstallAddress(desensitizedOwnerInstallAddress);
            }
            // 机主通讯地址
            String desensitizedOwnerMailingAddress = desensitizeAddress(entity.getOwnerMailingAddress());
            if (StrUtil.isNotBlank(desensitizedOwnerMailingAddress)) {
                entity.setOwnerMailingAddress(desensitizedOwnerMailingAddress);
            }
            // 经办人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getAgentName())) {
                entity.setAgentName(DesensitizedUtil.chineseName(entity.getAgentName()));
            }
            // 经办人身份证号
            if (IdcardUtil.isValidCard(entity.getAgentIdNumber())) {
                entity.setAgentIdNumber(DesensitizedUtil.idCardNum(entity.getAgentIdNumber(), 6, 4));
            }
            // 经办人证件地址
            String desensitizedAgentIdAddress = desensitizeAddress(entity.getAgentIdAddress());
            if (StrUtil.isNotBlank(desensitizedAgentIdAddress)) {
                entity.setAgentIdAddress(desensitizedAgentIdAddress);
            }
            // 经办人通讯地址
            String desensitizedAgentMailingAddress = desensitizeAddress(entity.getAgentMailingAddress());
            if (StrUtil.isNotBlank(desensitizedAgentMailingAddress)) {
                entity.setAgentMailingAddress(desensitizedAgentMailingAddress);
            }
            // 责任人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getPicName())) {
                entity.setPicName(DesensitizedUtil.chineseName(entity.getPicName()));
            }
            // 责任人身份证号
            if (IdcardUtil.isValidCard(entity.getPicIdNumber())) {
                entity.setPicIdNumber(DesensitizedUtil.idCardNum(entity.getPicIdNumber(), 6, 4));
            }
            // 责任人证件地址
            String desensitizedPicIdAddress = desensitizeAddress(entity.getPicIdAddress());
            if (StrUtil.isNotBlank(desensitizedPicIdAddress)) {
                entity.setPicIdAddress(desensitizedPicIdAddress);
            }
            // 责任人通讯地址
            String desensitizedPicMailingAddress = desensitizeAddress(entity.getPicMailingAddress());
            if (StrUtil.isNotBlank(desensitizedPicMailingAddress)) {
                entity.setPicMailingAddress(desensitizedPicMailingAddress);
            }
            // 单位证件地址
            String desensitizedCompanyIdAddress = desensitizeAddress(entity.getCompanyIdAddress());
            if (StrUtil.isNotBlank(desensitizedCompanyIdAddress)) {
                entity.setCompanyIdAddress(desensitizedCompanyIdAddress);
            }
            // 单位装机地址
            String desensitizedCompanyInstallAddress = desensitizeAddress(entity.getCompanyInstallAddress());
            if (StrUtil.isNotBlank(desensitizedCompanyInstallAddress)) {
                entity.setCompanyInstallAddress(desensitizedCompanyInstallAddress);
            }
            // 单位通讯地址
            String desensitizedCompanyMailingAddress = desensitizeAddress(entity.getCompanyMailingAddress());
            if (StrUtil.isNotBlank(desensitizedCompanyMailingAddress)) {
                entity.setCompanyMailingAddress(desensitizedCompanyMailingAddress);
            }
        });
    }

    /**
     * @Description 脱敏车联网卡敏感字段
     * @Auhtor 连新景
     * @Date 2024/09/26 15:15
     * @param list
     */
    public static void desensitizeIovCard(List<IovCardEntity> list) {
        list.forEach(entity -> {
            // 机主姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getOwnerName())) {
                entity.setOwnerName(DesensitizedUtil.chineseName(entity.getOwnerName()));
            }
            // 机主身份证号
            if (IdcardUtil.isValidCard(entity.getOwnerIdNumber())) {
                entity.setOwnerIdNumber(DesensitizedUtil.idCardNum(entity.getOwnerIdNumber(), 6, 4));
            }
            // 机主证件地址
            String desensitizedOwnerIdAddress = desensitizeAddress(entity.getOwnerIdAddress());
            if (StrUtil.isNotBlank(desensitizedOwnerIdAddress)) {
                entity.setOwnerIdAddress(desensitizedOwnerIdAddress);
            }
            // 机主通讯地址
            String desensitizedOwnerMailingAddress = desensitizeAddress(entity.getOwnerMailingAddress());
            if (StrUtil.isNotBlank(desensitizedOwnerMailingAddress)) {
                entity.setOwnerMailingAddress(desensitizedOwnerMailingAddress);
            }
            // 经办人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getAgentName())) {
                entity.setAgentName(DesensitizedUtil.chineseName(entity.getAgentName()));
            }
            // 经办人身份证号
            if (IdcardUtil.isValidCard(entity.getAgentIdNumber())) {
                entity.setAgentIdNumber(DesensitizedUtil.idCardNum(entity.getAgentIdNumber(), 6, 4));
            }
            // 经办人证件地址
            String desensitizedAgentIdAddress = desensitizeAddress(entity.getAgentIdAddress());
            if (StrUtil.isNotBlank(desensitizedAgentIdAddress)) {
                entity.setAgentIdAddress(desensitizedAgentIdAddress);
            }
            // 经办人通讯地址
            String desensitizedAgentMailingAddress = desensitizeAddress(entity.getAgentMailingAddress());
            if (StrUtil.isNotBlank(desensitizedAgentMailingAddress)) {
                entity.setAgentMailingAddress(desensitizedAgentMailingAddress);
            }
            // 责任人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getPicName())) {
                entity.setPicName(DesensitizedUtil.chineseName(entity.getPicName()));
            }
            // 责任人身份证号
            if (IdcardUtil.isValidCard(entity.getPicIdNumber())) {
                entity.setPicIdNumber(DesensitizedUtil.idCardNum(entity.getPicIdNumber(), 6, 4));
            }
            // 责任人证件地址
            String desensitizedPicIdAddress = desensitizeAddress(entity.getPicIdAddress());
            if (StrUtil.isNotBlank(desensitizedPicIdAddress)) {
                entity.setPicIdAddress(desensitizedPicIdAddress);
            }
            // 责任人通讯地址
            String desensitizedPicMailingAddress = desensitizeAddress(entity.getPicMailingAddress());
            if (StrUtil.isNotBlank(desensitizedPicMailingAddress)) {
                entity.setPicMailingAddress(desensitizedPicMailingAddress);
            }
            // 单位证件地址
            String desensitizedCompanyIdAddress = desensitizeAddress(entity.getCompanyIdAddress());
            if (StrUtil.isNotBlank(desensitizedCompanyIdAddress)) {
                entity.setCompanyIdAddress(desensitizedCompanyIdAddress);
            }
            // 单位通讯地址
            String desensitizedCompanyMailingAddress = desensitizeAddress(entity.getCompanyMailingAddress());
            if (StrUtil.isNotBlank(desensitizedCompanyMailingAddress)) {
                entity.setCompanyMailingAddress(desensitizedCompanyMailingAddress);
            }
        });

    }

    /**
     * @Description 脱敏短信端口敏感字段
     * @Auhtor 连新景
     * @Date 2024/09/26 15:15
     * @param list
     */
    public static void desensitizeSmsPort(List<SmsPortEntity> list) {
        list.forEach(entity -> {
            // 法人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getLegalPersonName())) {
                entity.setLegalPersonName(DesensitizedUtil.chineseName(entity.getLegalPersonName()));
            }
            // 法人身份证号
            if (IdcardUtil.isValidCard(entity.getLegalPersonIdNumber())) {
                entity.setLegalPersonIdNumber(DesensitizedUtil.idCardNum(entity.getLegalPersonIdNumber(), 6, 4));
            }
            // 经办人姓名
            if (Validator.isMatchRegex(CHINESE_NAME, entity.getAgentName())) {
                entity.setAgentName(DesensitizedUtil.chineseName(entity.getAgentName()));
            }
            // 经办人身份证号
            if (IdcardUtil.isValidCard(entity.getAgentIdNumber())) {
                entity.setAgentIdNumber(DesensitizedUtil.idCardNum(entity.getAgentIdNumber(), 6, 4));
            }
        });
    }

    /**
     * @Description 脱敏地址，如果county组有数据则保留到county，否则保存到city组
     * @Auhtor 连新景
     * @Date 2024/09/26 16:47
     * @param address
     * @return java.lang.String
     */
    private static String desensitizeAddress(String address) {
        if (StrUtil.isBlank(address)) {
            return null;
        }
        Matcher matcher = addressPattern.matcher(address);
        StringBuffer desensitizeAddress = new StringBuffer();
        if (matcher.matches()) {
            String province = matcher.group("province");
            String city = matcher.group("city");
            desensitizeAddress.append(province).append(city);
            String county = matcher.group("county");
            if (StrUtil.isNotBlank(county)) {
                desensitizeAddress.append(county);
            }
            desensitizeAddress.append("********");
        }
        return desensitizeAddress.toString();
    }
}
